from django.db import models
# Create your models here.
from celery import states
from django.utils.html import format_html


class TasksInfo(models.Model):
    ALL_STATES = sorted(states.ALL_STATES)
    TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES))

    IS_DELETED_CHOICES = (
        (1, "无效"),
        (0, "有效"),
    )

    task_id = models.CharField(verbose_name="任务id", max_length=256, unique=True, db_index=True, null=False, blank=False,
                               help_text="任务id")
    task_name = models.CharField(verbose_name="任务名称", null=True, blank=True, max_length=256, db_index=True,
                                 help_text="任务名称")
    operate_file = models.CharField(verbose_name="任务操作的文件", null=True, blank=True, max_length=256, db_index=True,
                                    help_text="任务操作的文件")
    task_args = models.TextField(verbose_name="所执行任务的位置参数", null=True, blank=True, help_text="Json格式存储的任务使用的位置参数")
    task_kwargs = models.TextField(verbose_name="所执行任务的关键词参数", null=True, blank=True, help_text="Json格式存储的任务使用的关键词参数")
    status = models.CharField(verbose_name="任务执行状态", max_length=50, default=states.PENDING, db_index=True,
                              choices=TASK_STATE_CHOICES, help_text="任务执行状态")
    result = models.TextField(verbose_name="任务执行结果", null=True, blank=True, default=None, editable=False,
                              help_text="任务执行结果")
    date_done = models.DateTimeField(verbose_name="任务完成时间", auto_now_add=True, db_index=True, help_text="任务完成时间")
    hostname = models.CharField(verbose_name="任务所在主机", max_length=100, null=True, blank=True, default=None,
                                editable=False, help_text="任务所在主机")
    traceback = models.TextField(verbose_name="错误栈", blank=True, null=True, help_text="错误栈")
    is_deleted = models.SmallIntegerField(verbose_name="是否删除", default=0, choices=IS_DELETED_CHOICES, help_text="是否删除")
    c_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True, help_text="创建时间")
    m_time = models.DateTimeField(verbose_name="修改时间", auto_now=True, help_text="修改时间")

    class Meta:
        db_table = "t_tasks_info"
        ordering = ['-date_done']
        verbose_name = "任务信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '<Task: {0.task_id} ({0.status})>'.format(self)

    def colored_status(self):
        if self.status == "SUCCESS":
            color = 'green'
        elif self.status == "FAILURE":
            color = 'red'
        elif self.status == "REVOKED":
            color = "pink"
        else:
            color = "black"

        return format_html(
            '<span style="color: {};">{}</span>',
            color,
            self.status
        )

    colored_status.short_description = "任务执行状态"

    def colored_is_deleted(self):
        if self.is_deleted == 1:
            color = "red"
        else:
            color = "green"

        return format_html(
            '<span style="color: {};">{}</span>',
            color,
            self.get_is_deleted_display()
        )

    colored_is_deleted.short_description = '任务是否被删除'

    def as_dict(self):
        return {
            'task_id': self.task_id,
            'task_name': self.task_name,
            'task_args': self.task_args,
            'task_kwargs': self.task_kwargs,
            'status': self.status,
            'result': self.result,
            'date_done': self.date_done,
            'traceback': self.traceback,
            'is_deleted': self.is_deleted,
            'c_time': self.c_time,
            'm_time': self.m_time,
        }
